# -*- coding: UTF-8 -*-
import requests
import cookielib

header={
        "Host": "www.zhihu.com",
        "Referer": "https://www.zhihu.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0"
    }

#使用登录session信息
session=requests.session();
session.cookies=cookielib.LWPCookieJar(filename="cookies")
try:
    session.cookies.load(ignore_discard=True)
except:
    print "cookies not load！"
    
#查看是否已经登录
def isLogin():
    url="https://www.zhihu.com/settings/profile"
    loginCode=session.get(url, headers=header, allow_redirects=False).status_code
    if 200 == loginCode :
        return True
    else:
        return False
    
#登录
def login():
    url="http://www.zhihu.com/login/phone_num"
    requestData={}
    requestData['phone_num']="15057126798"
    requestData['password']="YANGHUI123"
    requestData['remember_me']='true'
    try:
        r=session.post(url, data=requestData, headers=header);
        print r.text;
        print r.status_code
    except:
        print "异常出现"
    session.cookies.save()
   
#爬贴   
def spider(url):
    r=session.get(url, headers=header)
    print r.text
    
if isLogin() == False:
    login()
spider("https://www.zhihu.com/question/28278308/answer/40654262");

    